{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "f219baaa",
   "metadata": {},
   "source": [
    "---\n",
    "* 2023-10-23\n",
    "* 课程：Python语言\n",
    "* week07\n",
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b37008d0",
   "metadata": {},
   "source": [
    "# 本周内容\n",
    "\n",
    "## 认知数据结构（4种）\n",
    "\n",
    "> 1. 列表\n",
    "> 2. 元组\n",
    "> 3. 集合\n",
    "> 4. 字典\n",
    "> 5. \"一切皆是对象\"\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2e96f429",
   "metadata": {},
   "source": [
    "## 列表学习\n",
    "\n",
    "![](python-list.jpg)\n",
    "\n",
    "* Ordered: Maintain the order of the data insertion.\n",
    "* 保持数据插入的顺序**(有序性)**\n",
    "> 1. 列表中的元素可以被修改。我们可以在创建列表后向列表添加或删除项目。\n",
    "* Changeable: List is mutable and we can modify items.\n",
    "* 列表是可变的，我们可以修改条目**(可变性)**\n",
    "> 2. 列表中的项目是有序的。每个项都有一个惟一的索引值。新项目将被添加到列表的末尾。\n",
    "* Heterogeneous: List can contain data of different types\n",
    "* 列表可以包含不同类型的数据**(异构性)**\n",
    "> 3. 列表可以包含不同类型的元素，例如;它们可以包含字符串、整数、布尔或任何类型的元素。\n",
    "* Contains duplicate: Allows duplicates data\n",
    "* 允许重复的数据**(数据重复性)**\n",
    "> 4. 列表可以包含重复项，即列表可以有两个具有相同值的项。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ea9f1de1",
   "metadata": {},
   "source": [
    "### 创建列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "16daf450",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3]\n",
      "[1, 2, 3]\n",
      "[1.0, 'Jessa', 3]\n",
      "[]\n",
      "[]\n"
     ]
    }
   ],
   "source": [
    "# Using list constructor 使用构造器创建列表\n",
    "my_list1 = list((1, 2, 3))\n",
    "print(my_list1)\n",
    "# Output [1, 2, 3]\n",
    "\n",
    "# Using square brackets[] 直接使用[]创建列表\n",
    "my_list2 = [1, 2, 3]\n",
    "print(my_list2)\n",
    "# Output [1, 2, 3]\n",
    "\n",
    "# with heterogeneous items 创建不同类型数据结构的列表\n",
    "my_list3 = [1.0, 'Jessa', 3]\n",
    "print(my_list3)\n",
    "# Output [1.0, 'Jessa', 3]\n",
    "\n",
    "# empty list using list() 创建控列表\n",
    "my_list4 = list()\n",
    "print(my_list4)\n",
    "# Output []\n",
    "\n",
    "# empty list using [] 创建控列表\n",
    "my_list5 = []\n",
    "print(my_list4)\n",
    "# Output []"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d93b02da",
   "metadata": {},
   "source": [
    "### 列表的长度\n",
    "\n",
    "* 为了找到列表中存在的项数，我们可以使用len()函数。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "07a4df3b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n"
     ]
    }
   ],
   "source": [
    "my_list = [1, 2, 3]\n",
    "print(len(my_list))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "54751fb3",
   "metadata": {},
   "source": [
    "### 列表的取值和切片\n",
    "\n",
    "* Using **indexing**, we can access any item from a list using its index number\n",
    "* Using **slicing**, we can access a range of items from a list\n",
    "\n",
    "\n",
    "![](positive-and-negative-indexing.jpg)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "74797cf2",
   "metadata": {},
   "source": [
    "#### 取值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "792d35ea",
   "metadata": {},
   "outputs": [],
   "source": [
    "python_list = ['P','Y','T','H','O','N']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "c64fdcb8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'P'"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "python_list[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "8f408dbc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'N'"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "python_list[-1]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9d8f2243",
   "metadata": {},
   "source": [
    "#### 切片\n",
    "* listname[start_index : end_index : step]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "0c0c1e92",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['P', 'Y']"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "python_list[0:2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "37791e22",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['P', 'T', 'O']"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "python_list[0:5:2]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cd9a4890",
   "metadata": {},
   "source": [
    "### 列表的方法\n",
    "\n",
    "\n",
    "\n",
    "> 1. 增加\n",
    ">> 1. append() \n",
    ">>> 笔记：\n",
    ">> 2. insert() \n",
    ">>> 笔记：\n",
    "\n",
    "> 2. 删除\n",
    ">> 1. remove(item)\n",
    ">>> 笔记：\n",
    ">> 2. pop(index)\n",
    ">>> 笔记：\n",
    ">> 3. clear()\n",
    ">>> 笔记：\n",
    ">> 4. del\n",
    ">>> 笔记：\n",
    "\n",
    "> 3. 查询\n",
    ">> 1. 单个查询 find index()\n",
    ">> 2. 通过for循环查找指定值，使用in方法判断是否存在\n",
    ">> * 3. 通过枚举(enumerate)查到指定值\n",
    "\n",
    "\n",
    "> 4. 修改\n",
    ">> 1. find index() 通过index找到对应值进行修改\n",
    ">> 2. 通过查询指定值进行修改\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b2c8160d",
   "metadata": {},
   "source": [
    "## 课后作业\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bac54571",
   "metadata": {},
   "source": [
    "* 课本P67练习"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8cf97037",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.4"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "355.594px"
   },
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
